﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data;
using AgileShare.Framework.Library.Common;
using AgileShare.Framework.Core;

namespace AgileShare.Framework.Library.Helpers
{
	/// <summary>
	/// This class contains helper methods for working with SharePoint List Items
	/// - All methods should be static
	/// - DO NOT USE SPContext
	/// </summary>
	public class SPListItemHelper
	{
		/// <summary>
		/// Determines whether the list item has the specified field.
		/// </summary>
		/// <param name="listItem">The list item.</param>
		/// <param name="fieldTitleOrName">The Title, StaticName, or InternalName of the field.</param>
		/// <param name="nameVariation">Enum value indicating which field property to match on.</param>
		/// <returns>True if the field exists, otherwise false.</returns>
		public static bool HasField(SPListItem listitem, string fieldTitleOrName, Constants.SPFieldNameVariation nameVariation)
		{
			return listitem.Fields.Cast<SPField>().Any(f => (nameVariation == Constants.SPFieldNameVariation.Title && f.Title.Equals(fieldTitleOrName, StringComparison.CurrentCultureIgnoreCase))
															|| (nameVariation == Constants.SPFieldNameVariation.StaticName && f.StaticName.Equals(fieldTitleOrName, StringComparison.CurrentCultureIgnoreCase))
															|| (nameVariation == Constants.SPFieldNameVariation.InternalName && f.InternalName.Equals(fieldTitleOrName, StringComparison.CurrentCultureIgnoreCase)));
		}

		/// <summary>
		/// Determines whether the list item has the specified field.
		/// </summary>
		/// <param name="listItem">The list item.</param>
		/// <param name="fieldId">The Guid of the field.</param>
		/// <returns>True if the field exits, otherwise false</returns>
		public static bool HasField(SPListItem listItem, Guid fieldId)
		{
			return listItem.Fields.Cast<SPField>().Any(f => f.Id == fieldId);
		}

		/// <summary>
		/// Determines whether [has field value] [the specified field id].
		/// </summary>
		/// <param name="fieldId">The field id.</param>
		/// <param name="item">The item.</param>
		/// <returns>
		///   <c>true</c> if [has field value] [the specified field id]; otherwise, <c>false</c>.
		/// </returns>
		public static bool HasFieldValue(Guid fieldId, SPListItem item)
		{
			bool hasValue = (item.Fields.Contains(fieldId) && item[fieldId] != null);
			return hasValue;
		}

		/// <summary>
		/// Gets the default display form URL.
		/// </summary>
		/// <param name="listItem">The list item.</param>
		/// <param name="modal">if set to <c>true</c> [modal].</param>
		/// <returns></returns>
		public static string GetDefaultDisplayFormUrl(SPListItem listItem, bool modal = false)
		{
			var retVal = String.Empty;
			if (listItem != null)
			{
				retVal = String.Format("{0}{1}?ListId={2}&ID={3}&ContentTypeId={4}{5}"
									  , listItem.Web.Site.Url
									  , listItem.ParentList.DefaultDisplayFormUrl
									  , String.Concat("{", listItem.ParentList.ID, "}")
									  , listItem.ID
									  , listItem.ContentTypeId
									  , modal ? "&IsDlg=1" : String.Empty);
			}
			return retVal;
		}

		/// <summary>
		/// Tries the get list item.
		/// </summary>
		/// <param name="itemId">The list item id.</param>
		/// <param name="listId">The list id.</param>
		/// <param name="web">The web.</param>
		/// <returns></returns>
		public static SPListItem TryGetListItem(int itemId, Guid listId, SPWeb web)
		{
			SPListItem listItem = null;

			try
			{
				var list = SPListHelper.TryGetListByListId(listId, web);
				if (list != null)
				{
					listItem = list.GetItemById(itemId);
				}
			}
			catch (Exception ex)
			{
				string errorMessage = string.Format("Item not not found at web '{0}' with list ID '{1}' and ItemId '{2}'.", web.Url, listId, itemId);
				TraceProvider.Log(errorMessage, TraceProviderCategories.Default, (uint)TraceProviderEventIds.SPListItemHelper, TraceProviderSeverity.Information, ex);
			}

			return listItem;
		}


	}
}
